package com.fling.abcde.plugin;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.fling.abcde.base.BaseActivity;


/**
 * 类描述：代理插件的Activity
 * <p>
 * Created By FengLing 2019-12-27 027 15:48:21
 */
final public class HostPluginActivity extends BaseActivity implements View.OnClickListener {

    private static final String CLASS_NAME = "className";

    private BasePluginActivity mBasePluginActivity;

    public static void start(Context context,String className,String qwe) {

        start(context,className);
    }

    public static void start(Context context,String className) {

        Intent starter = new Intent(context,HostPluginActivity.class);
        if (!TextUtils.isEmpty(className)) {
            starter.putExtra(CLASS_NAME,className);
        }
        context.startActivity(starter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        String className = getIntent().getStringExtra(CLASS_NAME);
        try {
            mBasePluginActivity = ((BasePluginActivity) getClassLoader().loadClass(className).newInstance());
        } catch (Exception e) {
//                LogUtils.i("Host onCreate-->" + Log.getStackTraceString(e));
        }
        if (mBasePluginActivity != null) {
            mBasePluginActivity.setDelegate(this);
            mBasePluginActivity.onCreate(savedInstanceState);
        } else {
            TextView tv = new TextView(this);
            tv.setGravity(Gravity.CENTER);
            tv.setText("界面加载失败...");
            setContentView(tv,new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
        }
    }


    @Override
    public void onClick(View v) {

        if (mBasePluginActivity != null) {
            mBasePluginActivity.onClick(v);
        }
    }

    @Override
    protected void onStart() {

        super.onStart();
        if (mBasePluginActivity != null) {
            mBasePluginActivity.onStart();
        }
    }

    @Override
    protected void onResume() {

        super.onResume();
        if (mBasePluginActivity != null) {
            mBasePluginActivity.onResume();
        }
    }

    @Override
    protected void onPause() {

        super.onPause();
        if (mBasePluginActivity != null) {
            mBasePluginActivity.onPause();
        }
    }

    @Override
    protected void onStop() {

        super.onStop();
        if (mBasePluginActivity != null) {
            mBasePluginActivity.onPause();
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (mBasePluginActivity != null) {
            mBasePluginActivity.onSaveInstanceState(outState);
        }
    }

    @Override
    protected void onDestroy() {

        super.onDestroy();
        if (mBasePluginActivity != null) {
            mBasePluginActivity.setDelegate(null);
            mBasePluginActivity.onDestroy();
        }
    }
}